<?php
namespace app\finance\admin;
use app\system\admin\Admin;
use app\finance\model\Account as AccountModel;
use app\finance\model\BankInterturn as BankInterturnModel;
use think\Db;
class Bank extends Admin
{
    protected $hisiModel = 'BankInterturn';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名
	protected function initialize()
    {
        parent::initialize();
        if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $accounts = AccountModel::getSelect(AccountModel::where('id','<>',0)->where('status',1)->all());
            $this->assign('accounts', $accounts);
        }
    }
    public function getAccountBalance()
    {
        $id = $this->request->param('account_id');
        $AccountModel = new AccountModel;
        $balance = $AccountModel->getAccountBalance($id);
        echo $balance;die;
    }
    public function add()
    {
    	if ($this->request->isPost()) {

            $postData = $this->request->post();
            Db::startTrans();
            try {
            	$AccountModel = new AccountModel;
            	$balance = $AccountModel->getAccountBalance($postData['a_account']);
            	if($postData['a_account'] == $postData['b_account']){
            		throw new \Exception("转账账户不能想同", 1);

            	}
            	// if($balance < $postData['price']){
            	// 	throw new \Exception("账户余额不足", 1);
            	// }
            	$postData['createtime'] = time();
            	$postData['updatetime'] = time();
            	Db::name('bank_interturn')->insert($postData);

            	$out = [
					'sn' => $postData['sn'],
					'type' => 6,
					'account_id' => $postData['a_account'],
					'price' => $postData['price'],
					'createtime' => time(),
					'updatetime' => time(),
					'is_effective' => 1,
					'cate' => 2,
            	];
            	$in = [
					'sn' => $postData['sn'],
					'type' => 7,
					'account_id' => $postData['b_account'],
					'price' => $postData['price'],
					'createtime' => time(),
					'updatetime' => time(),
					'is_effective' => 1,
					'cate' => 1,
            	];
            	Db::name('system_balance_log')->insert($out);
            	Db::name('system_balance_log')->insert($in);
            	Db::commit();
            } catch (\Exception $e) {
            	Db::rollback();
            	return $this->error($e->getMessage());
            }
            return $this->success('保存成功', '');
        }

        $template = $this->request->param('template', 'form');

        return $this->fetch($template);
    }
    public function index()
    {
    	if ($this->request->isAjax()) {

            $where      = $data = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $type      = $this->request->param('type', 1);
            $a_account      = $this->request->param('a_account', '');
            $b_account      = $this->request->param('b_account', '');
            $bak      = $this->request->param('bak', '');
            if(!empty($a_account)){
                $where[] = ['a_account','=',$a_account];
            }
            if(!empty($b_account)){
                $where[] = ['b_account','=',$b_account];
            }
            if(!empty($bak)){
                $where[] = ['bak','like','%'.$bak.'%'];
            }
            if($type == 2){
                $data['data']   = BankInterturnModel::where($where)
                ->field('*,a_account as a_account_text, b_account as b_account_text')
                // ->page($page)->limit($limit)
                ->order('id asc')->select();

                $data['count']  = BankInterturnModel::where($where)->count('id');
                $data['code']   = 0;
                return json($data);
            }else{
                $data['data']   = BankInterturnModel::where($where)->field('*,a_account as a_account_text, b_account as b_account_text')->page($page)->limit($limit)->order('id asc')->select();

                $data['count']  = BankInterturnModel::where($where)->count('id');
                $data['code']   = 0;
                return json($data);
            }


        }
        $accounts = AccountModel::getSelect(AccountModel::where('id','<>',0)->where('status',1)->all());
        $this->assign('accounts', $accounts);
        return $this->fetch();
    }
}
